package com.mmy.controller.car;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mmy.controller.Common;
import com.mmy.controller.UtilConstants;
import com.mmy.domain.car.TCarBase;
import com.mmy.domain.car.TCarOrder;
import com.mmy.domain.car.TCarOrg;
import com.mmy.domain.car.TCarType;
import com.mmy.domain.car.VCarBase;
import com.mmy.domain.car.VCarOrder;
import com.mmy.domain.car.VCarPlan;
import com.mmy.domain.sys.TSysUser;
import com.mmy.service.car.impl.TCarBaseServiceImpl;
import com.mmy.service.car.impl.TCarOrderServiceImpl;
import com.mmy.service.car.impl.TCarOrgServiceImpl;
import com.mmy.service.car.impl.TCarPlanServiceImpl;
import com.mmy.service.car.impl.TCarTypeServiceImpl;

/**
 * 类的介绍：车辆信息
 *
 * @author shangzl
 * @date 创建日期：2020-08-06
 * @version 1.0
 */

@Controller
public class CarBaseController {

	@Autowired
	TCarBaseServiceImpl tCarBaseServiceImpl;
	@Autowired
	TCarTypeServiceImpl tCarTypeServiceImpl;
	@Autowired
	TCarOrgServiceImpl tCarOrgServiceImpl;
	@Autowired
	TCarOrderServiceImpl tCarOrderServiceImpl;
	@Autowired
	TCarPlanServiceImpl tCarPlanServiceImpl;
	@Autowired
	Common common;

	/**
	 * 函数功能说明： 跳转--车辆列表页
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 liys
	 * @date 创建日期2021.03.02
	 */
	@RequestMapping(value = "/carbase")
	public String ToCarBase(Model model) {
		
		List<VCarBase> lstCarBase = tCarBaseServiceImpl.findAll();
		List<TCarType> lstCarType = tCarTypeServiceImpl.findAll();
		List<TCarOrg> lstCarOrg = tCarOrgServiceImpl.findAll();
		model.addAttribute("carbaselist", lstCarBase);
		model.addAttribute("cartypelist", lstCarType);
		model.addAttribute("carorglist", lstCarOrg);
		return "webbackground/car/carbase";
	}

	@RequestMapping(value = "/createcarbase")
	public String ToCreateCarBase(Model model) {

		List<TCarType> lstCarType = tCarTypeServiceImpl.findAll();
		model.addAttribute("cartypelist", lstCarType);
		List<TCarOrg> lstCarOrg = tCarOrgServiceImpl.findAll();
		model.addAttribute("carorglist", lstCarOrg);

		return "webbackground/car/createcarbase";
	}

	@RequestMapping(value = "/carbaseinfo")
	public String ToCarBaseInfo(HttpServletRequest request, Model model) {

		int iCarID = Integer.parseInt(request.getParameter("iCarID"));

		VCarBase vCarBase = tCarBaseServiceImpl.findByID(iCarID);
		model.addAttribute("vCarBase", vCarBase);

		List<VCarOrder> lstCarOrder = tCarOrderServiceImpl.findByCarID(iCarID);
		model.addAttribute("orderlist", lstCarOrder);

		return "webbackground/car/carbaseinfo";
	}

	@RequestMapping(value = "/ordercarinfo")
	public String ToOrderCarInfo(HttpServletRequest request, Model model) {

		int iID = Integer.parseInt(request.getParameter("iID"));

		TCarOrder tCarOrder = tCarOrderServiceImpl.findByID(iID);
		model.addAttribute("tCarOrder", tCarOrder);

		String strOrderID = tCarOrder.getOrderID();
		int iPlanID = Integer.parseInt(strOrderID.substring(6, 12));

		VCarPlan vCarPlan = tCarPlanServiceImpl.findByID(iPlanID);

		model.addAttribute("vCarPlan", vCarPlan);

		return "webbackground/car/ordercarinfo";
	}

//收款打印页面
	@RequestMapping(value = "/printcar")
	public String ToPrintCar(HttpServletRequest request, Model model) {

		int iID = Integer.parseInt(request.getParameter("iID"));

		TCarOrder tCarOrder = tCarOrderServiceImpl.findByID(iID);
		model.addAttribute("tCarOrder", tCarOrder);

		return "webbackground/car/printcar";
	}

	// 退款打印页面
	@RequestMapping(value = "/printcancelcar")
	public String ToPrintCancelCar(HttpServletRequest request, Model model) {

		int iID = Integer.parseInt(request.getParameter("iID"));

		TCarOrder tCarOrder = tCarOrderServiceImpl.findByID(iID);
		model.addAttribute("tCarOrder", tCarOrder);

		return "webbackground/car/printcancelcar";
	}

	/**
	 * 函数功能说明： 出纳审核
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@RequestMapping(value = "/cashiercar")
	public String ToCashierCar(Model model) {
		try {
			List<VCarBase> lstCarBase = tCarBaseServiceImpl.findAll();
			model.addAttribute("slist", lstCarBase);

			return "OA/cashier/cashiercar";
		} catch (Exception e) {

		}
		return null;
	}

	@RequestMapping(value = "/cashiercarorder")
	public String ToCashierCarOrder(HttpServletRequest request, Model model) {

		int iCarID = Integer.parseInt(request.getParameter("iCarID"));

		List<VCarOrder> lstCarOrder = tCarOrderServiceImpl.findByCarID(iCarID);
		model.addAttribute("orderlist", lstCarOrder);

		model.addAttribute("iCarID", iCarID);

		return "OA/cashier/cashiercarorder";
	}

	@RequestMapping(value = "/entercheckcashiercar")
	public String EnterCheckCashierCar(HttpServletRequest request, HttpSession session, String checkList) {

		TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");

		String[] checklist = checkList.split(",");
		List<Integer> lstCashierID = new ArrayList<>();
		List<TCarOrder> lstCashier = new ArrayList<>();

		for (String str : checklist) {
			lstCashierID.add(Integer.parseInt(str));
			int i = Integer.parseInt(str);
			TCarOrder tCarOrder = tCarOrderServiceImpl.findByID(i);

			if (tCarOrder.getCashierState() == 0) {
				tCarOrder.setCashierState((byte) 1);
			} else if (tCarOrder.getCashierState() == 1) {
				tCarOrder.setCashierState((byte) 2);
			} else {
				tCarOrder.setCashierState((byte) 1);
			}
			tCarOrder.setCashierDate(new Date());
			tCarOrder.setCashierID(tSysUser.getUserID());

			lstCashier.add(tCarOrder);
		}

		tCarOrderServiceImpl.saveAll(lstCashier);

		return "OA/cashier/cashiercarorder";

	}

//查询
	@RequestMapping(value = "/searchcashiercar")
	public String FindCashierCarOrder(HttpServletRequest request, Model model) {

		int iCarID = Integer.parseInt(request.getParameter("iCarID"));
		String strCashierState = request.getParameter("CashierState");
		if (strCashierState == "") {

			List<VCarOrder> lstCarOrder = tCarOrderServiceImpl.findByCarID(iCarID);
			model.addAttribute("orderlist", lstCarOrder);
			model.addAttribute("iCarID", iCarID);
			return "OA/cashier/cashiercarorder";
		} else {
			List<VCarOrder> lstCarOrder = tCarOrderServiceImpl.findByCashierAndCarID(strCashierState, iCarID);
			model.addAttribute("orderlist", lstCarOrder);
			model.addAttribute("iCarID", iCarID);
			return "OA/cashier/cashiercarorder";
		}

	}

	/**
	 * 函数功能说明：会计对账
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@RequestMapping(value = "/accountcar")
	public String ToAccountCar(HttpServletRequest request, Model model) {

		List<VCarBase> lstCarBase = tCarBaseServiceImpl.findAll();
		model.addAttribute("slist", lstCarBase);

		return "OA/account/AccountCar";
	}

	@RequestMapping(value = "/accountcarorder")
	public String ToAccountCarOrder(HttpServletRequest request, Model model) {

		int iCarID = Integer.parseInt(request.getParameter("iCarID"));

		List<VCarOrder> lstCarOrder = tCarOrderServiceImpl.findByCarID(iCarID);
		model.addAttribute("orderlist", lstCarOrder);

		model.addAttribute("iCarID", iCarID);

		return "OA/account/accountcarorder";
	}

	@RequestMapping(value = "/entercheckaccountcar")
	public String EnterCheckAccountCar(HttpServletRequest request, String checkList, HttpSession session) {

		TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");

		String[] checklist = checkList.split(",");
		List<Integer> lstAccountID = new ArrayList<>();
		List<TCarOrder> lstAccount = new ArrayList<>();

		for (String str : checklist) {
			lstAccountID.add(Integer.parseInt(str));
			int i = Integer.parseInt(str);
			TCarOrder tCarOrder = tCarOrderServiceImpl.findByID(i);

			if (tCarOrder.getAccountState() == 0) {
				tCarOrder.setAccountState((byte) 1);
			} else if (tCarOrder.getAccountState() == 1) {
				tCarOrder.setAccountState((byte) 2);
			} else {
				tCarOrder.setAccountState((byte) 1);
			}
			tCarOrder.setAccountDate(new Date());
			tCarOrder.setAccountID(tSysUser.getUserID());

			lstAccount.add(tCarOrder);
		}

		tCarOrderServiceImpl.saveAll(lstAccount);

		return "OA/account/accountcarorder";

	}

//	结账汇款
	@RequestMapping(value = "/entercheckremittancecar")
	public String EnterCheckRemittanceCar(HttpServletRequest request, String checkList) {

		String[] checklist = checkList.split(",");
		List<Integer> lstRemittanceID = new ArrayList<>();
		List<TCarOrder> lstRemittance = new ArrayList<>();

		for (String str : checklist) {
			lstRemittanceID.add(Integer.parseInt(str));
			int i = Integer.parseInt(str);
			TCarOrder tCarOrder = tCarOrderServiceImpl.findByID(i);

			if (tCarOrder.getRemittanceState() == 0) {
				tCarOrder.setRemittanceState((byte) 1);
			} else {
				tCarOrder.setRemittanceState((byte) 0);
			}

			lstRemittance.add(tCarOrder);
		}

		tCarOrderServiceImpl.saveAll(lstRemittance);

		return "OA/account/accountcarorder";

	}

//查询
	@RequestMapping(value = "/searchaccountcar")
	public String FindAccountCar(HttpServletRequest request, Model model) {

		int iCarID = Integer.parseInt(request.getParameter("iCarID"));
		String strAccountState = request.getParameter("AccountState");

		if (strAccountState == "") {
			List<VCarOrder> lstCarOrder = tCarOrderServiceImpl.findByCarID(iCarID);
			model.addAttribute("orderlist", lstCarOrder);
			model.addAttribute("iCarID", iCarID);

			return "OA/account/accountcarorder";
		} else {
			List<VCarOrder> lstCarOrder = tCarOrderServiceImpl.findByAccountAndCarID(strAccountState, iCarID);
			model.addAttribute("orderlist", lstCarOrder);
			model.addAttribute("iCarID", iCarID);

			return "OA/account/accountcarorder";
		}

	}

	/**
	 * 函数功能说明： 新建车辆信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期2020-08-06
	 */

	@RequestMapping(value = "/subcreatecarbase")
	public String CreateCarBase(TCarBase tCarBase, HttpSession session) throws Exception {

		try {
			// 获取最大值
			tCarBase.setCarID(common.createBaseCode(UtilConstants.TableName.T_CAR_BASE));
			tCarBase.setCreateDate(new Date());
			TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
			tCarBase.setCreator(tSysUser.getUserName());
			tCarBaseServiceImpl.save(tCarBase.getCarID());
			tCarBaseServiceImpl.save(tCarBase);
			return "redirect:carbase";

		} catch (Exception e) {

		}
		return null;
	}

	/**
	 * 函数功能说明： 删除车辆信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期 2020-08-06
	 */

	@PostMapping("/delcarbase")
	@ResponseBody
	public String DelCarBase(String checkList) {

		String[] checklist = checkList.split(",");
		List<Integer> lstCarID = new ArrayList<>();

		for (String str : checklist) {
			lstCarID.add(Integer.parseInt(str));
		}

		tCarBaseServiceImpl.delCarBase(lstCarID);

		return "webbackground/car/carbase";
	}

	/**
	 * 函数功能说明： 修改车辆信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期 2020-08-06
	 */

	@RequestMapping(value = "/enterupdatecarbase")
	public String EnterUpdateCarBase(HttpServletRequest request, Model model) {

		int iCarID = Integer.parseInt(request.getParameter("iCarID"));

		TCarBase tCarBase = tCarBaseServiceImpl.findByCarID(iCarID);
		model.addAttribute("tCarBase", tCarBase);
		List<TCarType> lstCarType = tCarTypeServiceImpl.findAll();
		model.addAttribute("cartypelist", lstCarType);
		List<TCarOrg> lstCarOrg = tCarOrgServiceImpl.findAll();
		model.addAttribute("carorglist", lstCarOrg);
		return "webbackground/car/updatecarbase";

	}

	@RequestMapping(value = "/updatecarbase")
	public String UpdateCarBase(TCarBase tCarBase, HttpSession session) throws Exception {
		try {
//			 定义一个新的实体类对象 
			TCarBase dtCarBase = tCarBaseServiceImpl.findByCarID(tCarBase.getCarID());
			dtCarBase.setOrgID(tCarBase.getOrgID());
			dtCarBase.setTypeID(tCarBase.getTypeID());
			dtCarBase.setCarNum(tCarBase.getCarNum());
			dtCarBase.setCarBrand(tCarBase.getCarBrand());
			dtCarBase.setDriverName(tCarBase.getDriverName());
			dtCarBase.setDriverPhone(tCarBase.getDriverPhone());
			dtCarBase.setSafeYear(tCarBase.getSafeYear());
			dtCarBase.setCarSafe(tCarBase.getCarSafe());
			dtCarBase.setCarYear(tCarBase.getCarYear());
			dtCarBase.setPower(tCarBase.getPower());
			dtCarBase.setMemo(tCarBase.getMemo());
			dtCarBase.setModifyDate(new Date());
			TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
			dtCarBase.setReviser(tSysUser.getUserName());
			tCarBaseServiceImpl.save(dtCarBase);
			return "redirect:carbase";

		} catch (Exception e) {

		}
		return null;
	}

	/**
	 * 函数功能说明： 查询车辆信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期 2020-08-06
	 */
	@RequestMapping(value = "/searchcarbase")
	public String FindCarBase(HttpServletRequest request, Model model) {

		String strTypeName = request.getParameter("TypeName");
		String strOrgName = request.getParameter("OrgName");
		if (strTypeName == "" && strOrgName == "") {
			List<VCarBase> lstCarBase = tCarBaseServiceImpl.findAll();
			model.addAttribute("carbaselist", lstCarBase);
			List<TCarType> lstCarType = tCarTypeServiceImpl.findAll();
			model.addAttribute("cartypelist", lstCarType);
			List<TCarOrg> lstCarOrg = tCarOrgServiceImpl.findAll();
			model.addAttribute("carorglist", lstCarOrg);
			return "webbackground/car/carbase";
		} else {
			List<VCarBase> lstCarBase = tCarBaseServiceImpl.findByName(strTypeName, strOrgName);
			model.addAttribute("carbaselist", lstCarBase);
			List<TCarType> lstCarType = tCarTypeServiceImpl.findAll();
			model.addAttribute("cartypelist", lstCarType);
			List<TCarOrg> lstCarOrg = tCarOrgServiceImpl.findAll();
			model.addAttribute("carorglist", lstCarOrg);
			return "webbackground/car/carbase";
		}
	}

	@RequestMapping("/getCars")
	@ResponseBody
	public String getCars() {
		
		String res = tCarBaseServiceImpl.carSelect();
		return res;
	}
}
